<?php

/**
 * This is the model class for table "zy_vpnlist".
 *
 * The followings are the available columns in table 'zy_vpnlist':
 * @property integer $id
 * @property string $type
 * @property string $server
 * @property string $port
 * @property string $protocol
 * @property string $interface
 * @property string $ca
 * @property string $comment
 * @property string $remark1
 * @property string $remark2
 * @property string $remark3
 * @property integer $is_delete
 * @property string $remark
 * @property integer $creater
 * @property integer $updater
 * @property string $create_time
 * @property string $update_time
 */
class Vpnlist extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Vpnlist the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_vpnlist';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('is_delete, creater, updater,package_id', 'numerical', 'integerOnly'=>true),
			array('type, server, port, protocol, interface,  comment, remark1, remark2, remark3, remark', 'length', 'max'=>255),
			array('ca,create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type, server, port, protocol, interface, ca, comment, remark1, remark2, remark3, is_delete, remark, creater, updater, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('base','ID'),
			'type' => Yii::t('base','Type'),
			'server' => Yii::t('base','Server'),
			'port' => Yii::t('base','Port'),
			'protocol' => Yii::t('base','Protocol'),
			'interface' => Yii::t('base','Interface'),
			'ca' => Yii::t('base','Ca'),
			'comment' => Yii::t('base','Comment'),
			'package_id'=>Yii::t('base','VPN　SERVER'),
			'remark1' => 'Remark1',
			'remark2' => 'Remark2',
			'remark3' => 'Remark3',
			'is_delete' => 'Is Delete',
			'remark' => 'Remark',
			'creater' => Yii::t('base','Creater'),
			'updater' => Yii::t('base','Updater'),
			'create_time' =>Yii::t('base','Create Time'),
			'update_time' => Yii::t('base','Update Time'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('server',$this->server,true);
		$criteria->compare('port',$this->port,true);
		$criteria->compare('protocol',$this->protocol,true);
		$criteria->compare('interface',$this->interface,true);
		$criteria->compare('ca',$this->ca,true);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('remark1',$this->remark1,true);
		$criteria->compare('remark2',$this->remark2,true);
		$criteria->compare('remark3',$this->remark3,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('creater',$this->creater);
		$criteria->compare('updater',$this->updater);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('package_id',$this->package_id,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getvpnlist($user_service_id){
		$userservice=UserService::model()->find('user_service_id='.$user_service_id);
		$package_id=$userservice->package_id;
		$sql="select * from zy_vpnlist where package_id =".$package_id;
		$vpnlist=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($vpnlist,'id','comment');
	}
	public function getAllvpnlist(){
		$level=yii::app()->user->level;
		$user_id=yii::app()->user->id;
		if($level==1){
		$sql = "select * from zy_vpnlist";
		}else{
			$sql="SELECT *FROM zy_vpnlist WHERE id IN (SELECT b.field1 FROM zy_user_service a LEFT JOIN zy_login_list b ON a.`user_service_id`=b.user_service_id WHERE a.`user_id`=".$user_id." AND a.`package_id` IN (SELECT package_id FROM zy_package WHERE service_id IN
					(select service_id from zy_service where code = 'smartrouting')
					))";
		}
		$vpnlist=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($vpnlist,'id','comment');
	}
}